$(function() {
	$('.table-sort').DataTable(
			{
				order : [ [ 1, 'asc' ] ],
				ajax : {
					url : "/allbikes",
					type : 'GET',
					dataSrc : ""
				},
				columns : [  {
                    data : "id"
                }, {
					data : "id"
				}, {
					data : "status",
					defaultContent : ""
				}, {
					data : "location.1",
					defaultContent : ""
				}, {
					data : "location.0",
					defaultContent : ""
				}, {
					data : "desc",
					defaultContent : ""
				}, {
					data : "id",
					defaultContent : ""
				} ],
				columnDefs : [{
					targets : [ 0 ],
					orderable : false,
					render : function(id, type, row, meta) {
						return '<input id="input-' + id
								+ '" type="checkbox" name="ids" value=' + id
								+ '><label for="input-' + id + '"></label>';
					}
				}, {
                    targets: [6],
                    render: function(data, type, row, meta) {
                        return '<a title="编辑" href="javascript:;" onclick="bike_edit(' + data +')" style="text-decoration:none">' +
							'<i class="Hui-iconfont">&#xe6df;</i></a>' +
							'<a title="删除" href="javascript:;" onclick="bike_del(' + data +')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>'
                    }
                }]
			});

});

function bike_add(title, url, w, h) {
	layer_show(title, url, w, h);
}

function bike_dels() {
	var cks = $(".table-sort input[name='ids']:checked");
	layer.confirm('确认要删除吗？', function(index) {
		var param = [];
		cks.each(function() {
			param.push($(this).val());
		});
		$.ajax({
			url : "/bike/" + param,
			type : "delete",
			success: function(msg){
				location.reload();
				layer.close(index);
			}
		});
		
	});
}

function bike_del(id) {
	layer.confirm('确认要删除吗？', function(index) {
		$.ajax({
			url : "/bike/" + id,
			type : "delete",
			success: function(msg){
				location.reload();
				layer.close(index);
			}
		});
	});
}

function bike_edit(id) {

	console.log(id+"   "+typeof id);
	$.ajax({
		url : "/saveId?id=" + id,
		type : "get",
		dataType : "json",
		success : function(data) {
			console.log("存入session成功"+data);
		}
	});

	layer.open({
		type: 2,
		area: [800+'px', 510 +'px'],
		fix: false, //不固定
		maxmin: true,
		shade:0.4,
		title: "编辑单车信息",
		content: "/bike_edit",
		success: function(layero, index){
			// var body = layer.getChildFrame('body', index);
			// location.reload();
		}
	});
	
}
